<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/liangcanglogo.png" alt="" />
        <span>基于语音识别的智慧粮仓</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container class="home_container_xiamian">
      <!-- 侧边栏 -->
      <el-aside
        :width="isCollapse ? '64px' : '200px'"
        class="home_container_cebian"
      >
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
          class="cebianyuanjiao"
        >
          <el-menu-item index="/users" @click="saveNavState('/users')">
            <i class="el-icon-user"></i>
            <span slot="title">用户管理</span>
          </el-menu-item>
          <!--一级菜单-->
          <el-submenu index="2">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-menu"></i>
              <!--文本-->
              <span>设备管理</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/Device" @click="saveNavState('/Device')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-reading"></i>
                <!--文本-->
                <span>设备信息总览</span>
              </template>
            </el-menu-item>
            <!--二级菜单-->
            <el-menu-item
              index="/Addordelete"
              @click="saveNavState('/Addordelete')"
            >
              <template slot="title">
                <!--图标-->
                <i class="el-icon-warning-outline"></i>
                <!--文本-->
                <span>增删设备</span>
              </template>
            </el-menu-item>
            <!--二级菜单-->
            <el-menu-item
              index="/Equipmentlist"
              @click="saveNavState('/Equipmentlist')"
            >
              <template slot="title">
                <!--图标-->
                <i class="el-icon-tickets"></i>
                <!--文本-->
                <span>设备列表</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <!--一级菜单-->
          <el-submenu index="3">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-setting"></i>
              <!--文本-->
              <span>系统日志</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="3-4-1">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-reading"></i>
                <!--文本-->
                <span>日志</span>
              </template>
            </el-menu-item>
            <!--二级菜单-->
            <el-menu-item index="3-4-2">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-warning-outline"></i>
                <!--文本-->
                <span>信息</span>
              </template>
            </el-menu-item>
            <!--二级菜单-->
            <el-menu-item index="3-4-3">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-tickets"></i>
                <!--文本-->
                <span>系统</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 切换菜单折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-break: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
